{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fdbbe34e",
   "metadata": {},
   "source": [
    "-------\n",
    "* 记录人：袁玉婷\n",
    "* 时间：2021-11-29\n",
    "* week12\n",
    "* 学习资源：[python 官方网站](https://www.python.org/doc/)    \n",
    " [函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1c798a8",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "> 1.函数的定义：def  \n",
    "> 2.函数名的定义规则-》与变量名相同  \n",
    "> 3.调用函数的方法  \n",
    "> 4.参数：  \n",
    ">> A.形式参数  \n",
    ">> B.实际参数  \n",
    ">> C.有多个参数时：  \n",
    ">>> a.关键字参数  \n",
    ">>> b.位置参数（调用参数时）-》 关键字参数都要放在末尾（多个不考虑顺序）  \n",
    ">>> c.默认参数（定义函数时）-》 默认参数都要放在末尾（多个不考虑顺序）  \n",
    "\n",
    "# 本周知识\n",
    "\n",
    "> 1.函数的返回值  \n",
    "> 2.函数的参数类型  \n",
    "> 3.模块与函数的关系  \n",
    "> 4.定义一个模块，然后调用模块  \n",
    ">> A.调用模块  \n",
    ">> B.调用模块中的函数  \n",
    "\n",
    "> 5.模块与函数实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f9e3f08",
   "metadata": {},
   "source": [
    "# 函数的返回值\n",
    "\n",
    "> 1.函数一定要有返回值（无论是什么编程语言）  \n",
    "> 2.在其他的编程语言中（如c语言，Java等），返回值一定要写，不写会报错！对于python，返回值如果不写，给定默认值 None  \n",
    "> 3.返回值一般为函数体运行的结果（result），也可以是 自定义 的函数返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "74118982",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1：\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + '' + last_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "da67ec2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Lihua'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "34c3dfe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2：\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fcc1f4cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ade8a6e9",
   "metadata": {},
   "source": [
    "# 函数的参数\n",
    "\n",
    "## 函数输入的参数\n",
    "\n",
    "> 1.函数可以输入复杂的参数：  \n",
    ">> A.函数参数为列表  \n",
    ">> B.函数参数为字典  \n",
    "\n",
    "> 2.函数可以输入不确定个数的参数：\n",
    ">> A.* args  \n",
    ">> B.** kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ffd327c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aebaec34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lihua', 'xiaoming', 'ziqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"lihua\",\"xiaoming\",\"ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f831276c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3879e345",
   "metadata": {},
   "outputs": [],
   "source": [
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Cache-Control\": \"max-age=0\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61adbab5-474ba2083def2d53739648cb\"\n",
    "  }, \n",
    "  \"origin\": \"223.104.64.237\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8224cab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'max-age=0', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61adbab5-474ba2083def2d53739648cb'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9c4f063",
   "metadata": {},
   "source": [
    "> 2.函数可以输入不确定个数的参数：\n",
    ">> A.* args  \n",
    ">>> 1.可以接受多个数据（单值）  \n",
    ">>> 2.将多个数据存在一个**元组**当中，实际还是一个变量\n",
    "\n",
    ">> B.** kwargs\n",
    ">>> 1.可以接受多个数据(键值对)==》 关键值参数  \n",
    ">>> 2.将多个 键值对 的数据存放在一个字典中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "be59bdde",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args\n",
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8bc15d11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "235c6de6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs\n",
    "def person(stu_name,**stu_info):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stu_name,stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b48957b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('lihua', {'age': 18, 'job': 'student', 'addregss': '广州南方学院'})"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"lihua\",age=18,job=\"student\",addregss=\"广州南方学院\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "5552a7d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_name(arg1,arg2,*arg,**kwargs):\n",
    "    \"\"\"考虑比较周到的函数参数\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2ebb597",
   "metadata": {},
   "source": [
    "## 输出的返回值\n",
    "\n",
    "> 1.返回值可以是 字符串、数值、列表、字典 等数据（单一值）  \n",
    "> 2.返回值是唯一的，不可以有两个（这是其他语言，如c语言，Java等）  \n",
    "> 3.python可以输出多个返回值，结果会用一个元组进行封装并返回（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ee1c8cae",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b608a5b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "54d6975d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "24188e84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0afb16d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b5e7bbf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "717b1fbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值retur\"\"\"\n",
    "    return httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c52b8b4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Cache-Control': 'max-age=0',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adbab5-474ba2083def2d53739648cb'}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e065af4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试多个值\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,\"lihua\",[1,2,3],{\"name\":\"A\"}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "af2b5e66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'lihua', [1, 2, 3], {'name': 'A'})"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ce6f62",
   "metadata": {},
   "source": [
    "# 模块\n",
    "\n",
    "> 1.使用模块可以隐藏函数的源代码  \n",
    "> 2.使用的人不需要会底层代码（只需会使用模块即可）  \n",
    "> 3.调用模块的方法\n",
    ">> A.必须先调用模块，才可以使用\n",
    ">>> a.import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用的方法为：moudle_name.func_name()  \n",
    ">>> b.from moudle_name import func_name,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为 func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "f4427d56",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "c3d45995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=6, tm_hour=16, tm_min=35, tm_sec=32, tm_wday=0, tm_yday=340, tm_isdst=0)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "49b26729",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：调用整个模块\n",
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a62fc910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9141cf57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function news_链接 in module study:\n",
      "\n",
      "news_链接(url, 新闻分类, page)\n",
      "    抓取学校新闻的所有页面链接\n",
      "    url:学校网站的域名链接\n",
      "    新闻分类:学校新闻的分类英文简称\n",
      "    page:你想要抓取的页面尾页\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ce0d95a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#study.news_链接('https://www.nfu.edu.cn/','xydt','96')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83a051e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二： 调用模块中某一个函数\n",
    "from study import search4vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "74d848ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be396bfd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
